package com.clayecloud.module_identitycard;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
import android.view.View;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.clayecloud.module_identitycard.adapter.CardStackAdapter;
import com.clayecloud.module_identitycard.databinding.ActivityIdcardBinding;
import com.clayecloud.module_identitycard.entity.IDCard;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.OnTitleBarListener;
import com.loopeer.cardstack.CardStackView;
import com.loopeer.cardstack.UpDownStackAnimatorAdapter;

import org.json.JSONObject;

import java.util.Arrays;
import java.util.List;

@Route(path = "/idCard/index")
public class IDCardActivity extends AppCompatActivity {

    private ActivityIdcardBinding activityIdcardBinding;

    private IDCardActivityViewModel idCardActivityViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityIdcardBinding = DataBindingUtil.setContentView(this, R.layout.activity_idcard);
        //获取数据模型
        ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication());
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        idCardActivityViewModel = viewModelProvider.get(IDCardActivityViewModel.class);
        // 初始化函数进行初始化
        init();
        // 绑定监听事件
        initListener();
    }

    /**
     * 初始化函数
     */
    private void init() {
        // 控制状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        // 初始化数据
        idCardActivityViewModel.loadData();
        // 进行card装配
        activityIdcardBinding.cardStackView.setAdapter(new CardStackAdapter(this,idCardActivityViewModel.getIDCardList().getValue()));
        activityIdcardBinding.cardStackView.setItemExpendListener(new CardStackView.ItemExpendListener() {
            @Override
            public void onItemExpend(boolean expend) { }
        });
        activityIdcardBinding.cardStackView.setAnimatorAdapter(new UpDownStackAnimatorAdapter(activityIdcardBinding.cardStackView));
        // 进行对卡片的数据监听
        idCardActivityViewModel.getIDCardList().observe(this, new Observer<List<IDCard>>() {
            @Override
            public void onChanged(List<IDCard> idCardList) {
                activityIdcardBinding.cardStackView.setAdapter(new CardStackAdapter(activityIdcardBinding.getRoot().getContext(), idCardActivityViewModel.getIDCardList().getValue()));
            }
        });
    }

    /**
     * 绑定监听事件
     */
    public void initListener(){
        // 头部导航监听函数
        activityIdcardBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                finish();
                overridePendingTransition(R.anim.window_fake, R.anim.window_right_out);
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });
    }

    /**
     * 在删除此Activity时 进行操作防止内存泄漏
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.destroy(this, null);
    }

}